package com.faith.translation;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class TranslationActivity extends Activity{

	private EditText inputText;
	private Button translate;
	private TextView translation;
	private ProgressBar progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		inputText = (EditText)findViewById(R.id.inputText);
		translate = (Button)findViewById(R.id.btnTranlate);
		progress = (ProgressBar)findViewById(R.id.gettingTranslation);
		translation = (TextView)findViewById(R.id.resultTranslation);
		translate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!TextUtils.isEmpty(inputText.getText().toString())){
					progress.setVisibility(View.VISIBLE);
					new GetHTMLAsynTask().execute(inputText.getText().toString());
					rawHtml = "";
				}else{
					Toast.makeText(TranslationActivity.this, "Hãy nhập từ!", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	private class GetHTMLAsynTask extends AsyncTask<String, Void, Void>{
		
		@Override
		protected void onPreExecute() {
			
		}
		@Override
		protected Void doInBackground(String... params) {
			word = params[0];
			getDocs("http://tratu.soha.vn/index.php?search="+params[0].replace(" ", "+")+"&dict=en_vn&btnSearch=&chuyennganh=&tenchuyennganh=");
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			
		}
	}
	private String rawHtml = "";
	String word="";
	private void getDocs(String url) {
		Document doc=null;
		try {
			Log.e("getDoc","Starting get doc..");
			doc = Jsoup.parse(new URL(url),20000);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(doc==null){
			Log.e("","Get html is null");
		}else{
			Log.e("",doc.html());
		}
		Elements resultBox = doc.select("a.nodeco");
		if(resultBox!=null && resultBox.size()>0){
			Log.e("","Got one more result :"+resultBox.get(0).toString());//section-h5
			try {
				Log.e("getDoc","Starting get doc..");
				doc = Jsoup.parse(new URL(resultBox.get(0).toString().substring(resultBox.get(0).toString().indexOf("href=\"")+6,resultBox.get(0).toString().indexOf("\" target="))),20000);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			resultBox = doc.select("div.section-h5");
			rawHtml += "<b>Dịch sang Tiếng Việt: "+word.toUpperCase(Locale.US)+"</b><p/>"+resultBox.html();
			resultBox = doc.select("div.show-alter");
			rawHtml += resultBox.html();
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					translation.setText(Html.fromHtml(rawHtml));
					progress.setVisibility(View.INVISIBLE);
				}
			});
		}else{
			resultBox = doc.select("div.section-h5");
			rawHtml = "<b>Dịch sang Tiếng Việt: "+word.toUpperCase(Locale.US)+"</b><p/>"+resultBox.html();
			resultBox = doc.select("div.section-h2");
			rawHtml += resultBox.html();
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					if(rawHtml.equals("<b>Dịch sang Tiếng Việt: "+word.toUpperCase(Locale.US)+"</b><p/>")){
						Toast.makeText(TranslationActivity.this, "Không tìm thấy!", Toast.LENGTH_LONG).show();
						Log.e("","Không tìm thấy!");
					}else{
						translation.setText(Html.fromHtml(rawHtml));
					}
					progress.setVisibility(View.INVISIBLE);
				}
			});
		}
	}
}
